const signIn = document.getElementById("signInBtn");
const signInContent = document.getElementById("noLogin");
const signOut = document.getElementById("signOutBtn");
const signOutContent = document.getElementById("isLogin");
const nickname = document.getElementById("nickname");
const email = document.getElementById("email");
const avatar = document.getElementById('avatar')
let currentUser = "";
chrome.storage.sync.get('isLogin', (data) => {
    isLogin = data.isLogin;
    showMenu()
})

chrome.storage.sync.get('sessionID', (data) => {
    sessionID = data.sessionID;
})

let pageUrl = '';
(async function initPopupWindow() {
    let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
    if (tab?.url) {
        try {
            let url = new URL(tab.url);
            pageUrl = url.hostname;
        } catch { }
    }
})();



function showMenu() {
    if (isLogin) {
        signOutContent.style.display = 'block'
        signInContent.style.display = 'none'
        chrome.storage.sync.get('userInfo', (data) => {
            currentUser = JSON.parse(data.userInfo);
            nickname.innerHTML = currentUser.nickname
            email.innerHTML = currentUser.email
            avatar.src = currentUser.icon
        })
    } else {
        signInContent.style.display = 'block'
        signOutContent.style.display = 'none'
    }
};
signIn.onclick = async () => {
    chrome.runtime.sendMessage({ currentStep: "getUserInfo" });
}

signOut.onclick = async () => {
    isLogin = !isLogin
    chrome.storage.sync.set({ "isLogin": isLogin }, () => {
        showMenu()
        chrome.runtime.sendMessage({ currentStep: "loginOut" }, () => {
            window.close()
        });
    })
};

